package com.example.dao.daoImple;

import com.example.dao.UserDao;
import com.example.entity.User;
import com.example.util.JDBCUtil;

public class UserDaoImpl implements UserDao {

    //根据id查询用户信息
    @Override
    public User queryUserById(Long id) {
        String sql = "SELECT id,user_name,nick_name,avatar,likes,fans,focus,`describe`,certification,address FROM tb_user WHERE id = ? AND state = 0";
        return JDBCUtil.getInstance(User.class, sql, id);
    }

    @Override
    public User queryUserByUsername(String username) {
        String sql = "SELECT id,user_name,nick_name,avatar,likes,fans,focus,`describe`,certification,address,email FROM tb_user WHERE (user_name = ? OR email = ?) AND state = 0";
        User instance = JDBCUtil.getInstance(User.class, sql, username, username);
        return instance == null ? null : instance;
    }

    @Override
    public User loginUser(String userName, String password) {
        String sql = "SELECT id,user_name,nick_name,avatar,likes,fans,focus,`describe`,certification,address,email FROM tb_user WHERE (user_name = ? OR email = ?) AND password = ? AND state = 0";
        User user = JDBCUtil.getInstance(User.class, sql, userName,userName, password);
        if (user != null) {
            return user;
        }
        return null;
    }

    @Override
    public boolean registerUser(String nickname, String username, String password, String email) {
        String sql = "INSERT INTO tb_user (user_name,PASSWORD,nick_name,email) VALUES (?,?,?,?)";
        int re = JDBCUtil.ALLCUD(sql,username,password,nickname,email);
        if(re==1){
            return true;
        }
        return false;
    }

    @Override
    public User queryUserByNameEmail(String username, String email) {
        String sql = "SELECT id,user_name,nick_name,avatar,likes,fans,focus,`describe`,certification,address,email FROM tb_user WHERE (user_name = ? OR email = ? ) AND email != '' AND state = 0";
        User user = JDBCUtil.getInstance(User.class, sql, username, email);
        if (user != null) {
            return user;
        }
        return null;
    }



    @Override
    public int updateUserById(User user) {
        String sql = "UPDATE tb_user SET nick_name = ?, `describe` = ?, address = ?,avatar = ? WHERE id = ?";
        return JDBCUtil.ALLCUD(sql,user.getNickName(),user.getDescribe(),user.getAddress(),user.getAvatar(),user.getId());

    }

}
